﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TY_Hoist.WinApp.Code;
using System.Threading;
using System.Threading.Tasks;

namespace TY_Hoist.WinApp
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        RemoteControl remoteControl;
        private void button1_Click(object sender, EventArgs e)
        {
            remoteControl.MotorRight(true);
            remoteControl.Clear();
            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < 200; i++)
                {
                    Thread.Sleep(50);
                    this.Invoke(new Action(() =>
                    {
                        //if (remoteControl.SearchStop() == true)
                        //{
                        //    remoteControl.MotorLeft(false);
                        //}
                        this.textBox1.Text = remoteControl.SearchStart().ToString();


                    }));
                }
                remoteControl.MotorRight(false);


            });
        }
        private static byte[] strToToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            remoteControl = new RemoteControl();



        }

        private void button2_Click(object sender, EventArgs e)
        {
            remoteControl.MotorRight(true);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            remoteControl.MotorRight(false);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            remoteControl.MotorLeft(true);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            remoteControl.MotorLeft(false);
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            remoteControl.Dispose();
        }
    }
}
